<?
include('../rp2/global_functions.php');

include('wallet_db.html');

$availability=translate('availability');
$error_1=translate('error_1');
$error_2=translate('error_2');
$error_3=translate('error_3');
$database_error=translate('database_error');
$request_queued=translate('request_queued');
$exchange_options=translate('exchange_options');
$suma=translate('suma');
$will_exchange=translate('will_exchange');
$in=translate('in');
$submit=translate('submit');


//------------------------------------------------------
$sql['type']='SELECT';
$sql['params']='*';
$sql['table']='wallet';
$sql['where']="reseller_id='$id'";
$sql['options']='ORDER BY currency ASC';
$sql['return']='struct';
//------------------------------------------------------
if ($result=sql_exec($sql,$dbh,'assoc')) {
        echo "$availability";
        $available=array();
        foreach ($result as $value) {
                echo "$value[amount] $value[currency]; ";
                $available[$value[currency]]=$value[amount];
        }
        $currencies_available=array_keys($available);
} else {
	echo "<h4>$wallet_empty</h4>";
}

if (isset($_POST[hid_var_xchange]) && $_POST[hid_var_xchange]==475) {
        if (!preg_match("/^\d*$/", $_POST[amount])) {
                echo "<h4><font color=red>$error_1</font></h4>";
        } elseif ($available[$_POST[currency_from]] < $_POST[amount]) {
                echo "<h4><font color=red>$error_2</font></h4>";
        } elseif ($_POST[currency_from]==$_POST[currency_to]) {
                echo "<h4><font color=red>$error_3</font></h4>";
        } else {
        $currency_to=strtolower($_POST[currency_to]);
        //------------------------------------------------------
                $sql['type']='SELECT';
                $sql['params']="$currency_to";
                $sql['table']='currency';
                $sql['where']="curr='$_POST[currency_from]'";
                $sql['options']='';
                $sql['return']='struct';
        //------------------------------------------------------
        if (!$result=sql_exec($sql,'','assoc')) {
	                $errorMsg = translate('int_error');
									include_once('errorMsg.html');
									exit;
        }
        foreach ($result as $value) {
                foreach ($value as $key => $v) {
                        $tocurr=strtoupper($key);
                        $tomoney=$v;
                }
        }
        $rate="1 $_POST[currency_from] = $tomoney $tocurr";
        $currency_to=strtolower($_POST[currency_to]);
        //------------------------------------------------------
                $sql['type']='SELECT';
                $sql['params']="$currency_to as result";
                $sql['table']='currency';
                $sql['where']="curr='$_POST[currency_from]'";
                $sql['options']='';
                $sql['return']='struct';
        //------------------------------------------------------
        if ($result=sql_exec($sql,'','assoc')){
                $coefficient=$result[0][result];
                $new_amount=$_POST[amount]*$coefficient;
                $sql="INSERT INTO temp_currency
                        (reseller_id,order_date,amount_from,currency_from,amount_to,currency_to,approved, rate)
                        VALUES('$id',now(),'$_POST[amount]','$_POST[currency_from]',
                                '$new_amount','$_POST[currency_to]','n','$rate')";
                if ($result=exec_query($sql)) {
									  $okMsg = translate('$request_queued');
									  include_once('okMsg.html');
//                  echo "<h4><font color=green>$request_queued</font></h4>";
                } else {
	                $errorMsg = translate('int_error');
									include_once('errorMsg.html');
									exit;
                }
        } else {
	        $errorMsg = translate('int_error');
					include_once('errorMsg.html');
					exit;
        }

        }
}
unset($sql);
//------------------------------------------------------
        $sql['type']='SELECT';
        $sql['params']='curr';
        $sql['table']='currency';
        $sql['where']="";
        $sql['options']='';
        $sql['return']='struct';
//------------------------------------------------------
if ($result=sql_exec($sql,'','assoc')){
        foreach($result as $value) {
                $currency[]=$value[curr];
        }
} else {
	        $errorMsg = translate('int_error');
					include_once('errorMsg.html');
					exit;
}
$params[sid] = $sid;
$params[action] = "currencyExchange";
$url = "$baseURL/$webDIR/?".encode_params($params);

echo "<br><br><table class='bTable' cellspacing=0>
<form action=\"$url\" method=\"POST\">
<tr>
        <td colspan=2 align=center><b>$exchange_options</b></td>
</tr>
<tr>
        <td colspan=2 align=center>$suma&nbsp<input type=text name=amount size=5 maxlength=4 value=\"$_POST[amount]\">&nbsp;<select name=currency_from>";
        foreach ($currency as $value) {
                if ($_POST[currency_from]==$value) {
                        $ch='selected';
                } else {
                        $ch='';
                }
                if (in_array($value,$currencies_available)) echo "<option value=$value $ch>$value</option>\n";
        }
        echo "</select>&nbsp;$in&nbsp;<select name=currency_to>";
        foreach ($currency as $value) {
                if ($_POST[currency_to]==$value) {
                        $ch='selected';
                } else {
                        $ch='';
                }
                echo "<option value=$value $ch>$value</option>\n";
        }
        echo "</select>&nbsp;<input type=submit name=submit value=\"$submit\"><input type=hidden name=hid_var_xchange value=475></td>
</tr>
</form>
</table><br><br>";

//------------------------------------------------------
        $sql['type']='SELECT';
        $sql['params']='*';
        $sql['table']='currency';
        $sql['where']="";
        $sql['options']='';
        $sql['return']='struct';
//------------------------------------------------------
if ($result=sql_exec($sql,'','assoc')){
        foreach ($result as $value) {
                $curr=$value[curr];
                echo "<font size='-2' color=red>1 $curr $will_exchange";
                array_shift($value);
                foreach ($value as $key => $v ) {
                        if (strtolower($curr)!=$key) {
                                $key=strtoupper($key);
                                echo "$v $key; ";
                        }
                }
                echo "</font><br>";
        }
} else {
	        $errorMsg = translate('int_error');
					include_once('errorMsg.html');
					exit;
}


?>
